package com.ysxsoft.wdg.pay;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.util.ScreenUtils;


/**
 * 支付
 */
public class PayPopWindowWithCoupon extends PopupWindow implements View.OnClickListener {

    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;

    private LinearLayout llWx, llYuE, llZfb, couponLayout;
    private ImageView ivWx, ivZfb, ivYuE, ivFinish;
    private TextView tvMoney, btnCommit, tvYuE;
    private TextView counponMoney;

    private ImageView[] imageViews;


    private String paymoney;
    private int payType = 0;

    private OnPayLiatener onPayLiatener;

    private String money;

    private int price;//减去的金额

    public void setOnPayLiatener(OnPayLiatener onPayLiatener) {
        this.onPayLiatener = onPayLiatener;
    }

    public PayPopWindowWithCoupon(Context context, String paymoney) {
        super(context);
        mContext = context;
        this.paymoney = paymoney;
        initPopup();
        bindView();
        initView();
        setListener();
    }

    public void setMoney(String money) {
        this.money = money;
        counponMoney.setText(money == null ? "" : money);
    }

    public void setPrice(double price) {
        this.money = money;
        counponMoney.setText(money == null ? "" : money);
        double d=Double.valueOf(paymoney);
        double result=d-price;//减去优惠券金额
        paymoney=result+"";
        tvMoney.setText(mContext.getString(R.string.price_rmb, paymoney));
    }

    private void initPopup() {
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        ScreenUtils.getScreenWidth(mContext);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    private void bindView() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.pop_zhifu_with_coupon, null);
        tvMoney = rootView.findViewById(R.id.tv_money);
        counponMoney = rootView.findViewById(R.id.coupon);
        tvYuE = rootView.findViewById(R.id.tv_yuE);
        couponLayout = rootView.findViewById(R.id.couponLayout);

        ivYuE = rootView.findViewById(R.id.iv_yuE_choose);
        ivZfb = rootView.findViewById(R.id.iv_zfb_choose);
        ivWx = rootView.findViewById(R.id.iv_wx_choose);
        ivFinish = rootView.findViewById(R.id.iv_finish);

        llYuE = rootView.findViewById(R.id.ll_pay_yuE);
        llWx = rootView.findViewById(R.id.ll_pay_wx);
        llZfb = rootView.findViewById(R.id.ll_pay_zfb);
        btnCommit = rootView.findViewById(R.id.btn_commit);

        llYuE.setVisibility(View.VISIBLE);
        setContentView(rootView);
    }

    private void initView() {
        tvMoney.setText(mContext.getString(R.string.price_rmb, paymoney));
        imageViews = new ImageView[]{ivYuE, ivZfb, ivWx};
//        tvYuE.setText("余额(" +yuE+ "元)");
        setSelected(0);
    }

    private void setListener() {
        llYuE.setOnClickListener(this);
        llWx.setOnClickListener(this);
        llZfb.setOnClickListener(this);
        btnCommit.setOnClickListener(this);
        ivFinish.setOnClickListener(this);
        couponLayout.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_pay_yuE://余额支付
                setSelected(0);
                break;
            case R.id.ll_pay_zfb://支付宝支付
                setSelected(1);
                break;
            case R.id.ll_pay_wx://微信支付
                setSelected(2);
                break;
            case R.id.btn_commit://去支付
                onPayLiatener.payType(payType,paymoney);
                dismiss();
                break;
            case R.id.iv_finish:
                dismiss();
                break;
            case R.id.couponLayout:
//              选择优惠券
                if (onPayLiatener != null) {
                    onPayLiatener.onCouponClick();
                }
                break;
        }
    }

    private void setSelected(int position) {
        payType = position;
        for (int i = 0; i < imageViews.length; i++) {
            if (i == position) {
                imageViews[i].setSelected(true);
            } else {
                imageViews[i].setSelected(false);
            }
        }
    }

    //显示popWindow
    public void show(View view) {
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams attributes = ((Activity) mContext).getWindow().getAttributes();
        attributes.alpha = 0.8f;
        ((Activity) mContext).getWindow().setAttributes(attributes);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams attributes = ((Activity) mContext).getWindow().getAttributes();
        attributes.alpha = 1f;
        ((Activity) mContext).getWindow().setAttributes(attributes);
    }

    public interface OnPayLiatener {
        void payType(int payType,String totalMoney);

        void onCouponClick();
    }
}
